﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;

namespace BelleApp
{
    public partial class DisplayFullDetails : Form
    {
        string QuotationPath = "";
        ArrayList List = new ArrayList();
        ArrayList List2 = new ArrayList();
        DBManager db = new DBManager();
        Quotation q = new Quotation();
        decimal shippingcost = 0;
        decimal total = 0;
        decimal totalshipping = 0;
   
        //private void btnBrowse_Click(object sender, EventArgs e)
        //{
        //    OpenFileDialog ofd = new OpenFileDialog();
        //    ofd.Multiselect = false;
        //    ofd.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";

        //    if (ofd.ShowDialog() == DialogResult.OK)
        //    {
        //        //Now display the filename in the textbox.
        //        textBox3.Text = ofd.FileName;
        //        QuotationPath = textBox3.Text;
                
        //    }

        //}

        public DisplayFullDetails(int RFQID)
        {
            InitializeComponent();
        
            DBManager db = new DBManager();
            lblRFQvalue.Text = RFQID.ToString();
            List = db.SearchRFQ(RFQID);
            foreach (RFQ rfq in List) 
            {
                lblcustomervalue.Text = rfq.CustomerID.ToString();
            }
            List2 = db.DisplayRFQ(RFQID);
            dataGridView1.DataSource = List2;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text=="") 
                {
                    MessageBox.Show("Please fill in the Quotation Number!");
                
                }
                if (textBox2.Text == "")
                {

                    MessageBox.Show("Please fill in the Quotation Date!");
                }
                else
                {

                    //string docPath = @"C:\Users\KOKKIAN\Desktop\BelleApp\BelleApp\Quotation.xml";
                    string docPath = "C:\\SharedDatabase\\Quotation_S2.xml";
                    string QuoNum = textBox1.Text;
                    string QuoDate = textBox2.Text;

                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    XmlWriter wrt = XmlWriter.Create(docPath, settings);
                    wrt.WriteStartDocument();
                    wrt.WriteComment("This Document contains specific quotation information");
                    wrt.WriteStartElement("Quotations");
                    wrt.WriteAttributeString("QuotationNumber", QuoNum);
                    wrt.WriteAttributeString("Date", QuoDate);
                    wrt.WriteElementString("SellerID", "4321");
                    wrt.WriteElementString("BuyerID", lblcustomervalue.Text);
                    wrt.WriteElementString("RFQNumber", lblRFQvalue.Text);
                    wrt.WriteStartElement("Items");
                    decimal itemtotalprice = 0;
                    foreach (Quotation pc in List2)
                    {
                        wrt.WriteStartElement("Item");
                        wrt.WriteAttributeString("Id", pc.Name);
                        wrt.WriteElementString("ItemQty", pc.Quantity.ToString());
                        wrt.WriteElementString("ItemPrice", pc.askingprice.ToString());
                        wrt.WriteElementString("UnitPrice", pc.UnitPrice.ToString());
                        decimal totalprice = (Convert.ToDecimal(pc.UnitPrice.ToString())*Convert.ToDecimal(pc.Quantity.ToString()));
                        itemtotalprice += totalprice;
                        wrt.WriteElementString("TotalPrice", totalprice.ToString());
                        wrt.WriteEndElement();

                        if (pc.Quantity > 50)
                        {
                            shippingcost += 50 * 1;
                            double SP = pc.Quantity - 50;
                            //shippingcost = Convert.ToDecimal(SP * 1.5);
                            decimal extra = Convert.ToDecimal(SP * 1.5);
                            totalshipping += shippingcost + extra;
                        }
                        else
                        {
                            shippingcost = pc.Quantity * 1;
                            totalshipping += shippingcost;
                        }
                        total += (shippingcost + (pc.Quantity * pc.UnitPrice));
                        shippingcost = 0;
                    }
                    wrt.WriteEndElement();
                    wrt.WriteElementString("ItemTotalPrice", itemtotalprice.ToString());
                    wrt.WriteElementString("ShippingCharges", totalshipping.ToString());
                    wrt.WriteElementString("TotalCharges", total.ToString());

                    wrt.WriteEndElement();
                    wrt.Close();

                    MessageBox.Show("Generated successfully");
                    SearchRFQ rfq = new SearchRFQ();
                    rfq.Show();
                    this.Hide();
                }
            
            }catch(Exception ex)
            {

                throw ex;

            }finally
            {
            
            
            
            }
        
        }
        
    
    
    
        
    
    
    
        

       



    }
}
